package com.zhiche.lisa.bms.controller.utils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.model.bill.Bill;
import com.zhiche.lisa.bms.dao.model.bill.BillDetail;
import com.zhiche.lisa.bms.dao.model.capital.CapitalArrDtl;
import com.zhiche.lisa.bms.dao.model.capital.CapitalArrangement;
import com.zhiche.lisa.bms.dao.model.fee.FeePayable;
import com.zhiche.lisa.bms.dao.model.pay.PayAffirm;
import com.zhiche.lisa.bms.dao.model.pay.PayAffirmApply;
import com.zhiche.lisa.bms.dao.model.pay.PayApply;
import com.zhiche.lisa.bms.dao.model.pay.PayApplyDtlBill;
import com.zhiche.lisa.bms.enums.FeePayableBaseEnum;
import com.zhiche.lisa.bms.service.bill.BillDetailService;
import com.zhiche.lisa.bms.service.bill.BillService;
import com.zhiche.lisa.bms.service.capital.CapitalArrDtlService;
import com.zhiche.lisa.bms.service.capital.CapitalArrangementService;
import com.zhiche.lisa.bms.service.fee.FeePayableService;
import com.zhiche.lisa.bms.service.pay.PayAffirmApplyService;
import com.zhiche.lisa.bms.service.pay.PayAffirmService;
import com.zhiche.lisa.bms.service.pay.PayApplyDtlBillService;
import com.zhiche.lisa.bms.service.pay.PayApplyService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.RestfulResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author: linbao
 * @date: 2019-06-12
 */
@RestController
@RequestMapping("/updateFee")
public class FeePayableApplyCommonController {

    @Autowired
    private PayApplyService payApplyService;

    @Autowired
    private PayApplyDtlBillService payApplyDtlBillService;

    @Autowired
    private BillService billService;

    @Autowired
    private BillDetailService billDetailService;

    @Autowired
    private FeePayableService feePayableService;

    @Autowired
    private CapitalArrDtlService capitalArrDtlService;

    @Autowired
    private CapitalArrangementService capitalArrangementService;

    @Autowired
    private PayAffirmApplyService payAffirmApplyService;

    @Autowired
    private PayAffirmService payAffirmService;

    @PostMapping("/updateFeePayableOther")
    public RestfulResponse<String> updateFeePayableOther() {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        EntityWrapper<PayApply> ew = new EntityWrapper<>();
        ew.setSqlSelect("id");
        ew.orderBy("id asc");
        // List<Long> idList = Lists.newArrayList();
        // idList.add(98L);
        // ew.in("id", idList);
        List<Object> idObjList = payApplyService.selectObjs(ew);

        EntityWrapper<PayApplyDtlBill> dtlBillEw = new EntityWrapper<>();
        dtlBillEw.in("head_id", idObjList);
        List<PayApplyDtlBill> payApplyDtlBillList = payApplyDtlBillService.selectList(dtlBillEw);
        payApplyDtlBillList.forEach(payApplyDtlBill -> {
            PayApply payApply = payApplyService.selectById(payApplyDtlBill.getHeadId());

            // 账单
            EntityWrapper<Bill> billEW = new EntityWrapper<>();
            billEW.eq("id", payApplyDtlBill.getBilId());
            billEW.eq("is_delete", 0);
            billEW.eq("bill_type", "20");
            billEW.setSqlSelect("id");
            List<Object> billIdObjsList = billService.selectObjs(billEW);

            EntityWrapper<BillDetail> billDetailEW = new EntityWrapper<>();
            billDetailEW.in("bill_id", billIdObjsList);
            billDetailEW.setSqlSelect("distinct fee_id ");
            List<Object> billDetailFeeIdList = billDetailService.selectObjs(billDetailEW);

            EntityWrapper<FeePayable> feeEW = new EntityWrapper<>();
            feeEW.in("id", billDetailFeeIdList);
            List<FeePayable> feePayableList = feePayableService.selectList(feeEW);
            feePayableList.forEach(feePayable -> {
                if (FeePayableBaseEnum.NO.getCode().equals(feePayable.getIsPay())) {
                    feePayable.setIsPay(FeePayableBaseEnum.YES.getCode());
                    feePayable.setPayCode(payApply.getPayApplyCode());
                    feePayable.setPayDate(payApply.getApplyDate());
                    feePayable.setGmtModify(new Date());
                }
            });

            // 是否排款
            EntityWrapper<CapitalArrDtl> arrDtlEntityWrapper = new EntityWrapper<>();
            arrDtlEntityWrapper.eq("pay_id", payApply.getId());
            arrDtlEntityWrapper.orderBy("id asc");
            // arrDtlEntityWrapper.setSqlSelect("distinct head_id");
            List<CapitalArrDtl> capitalArrDtlList = capitalArrDtlService.selectList(arrDtlEntityWrapper);
            Set<Long> arrDtlIdSet = new HashSet<>();
            capitalArrDtlList.forEach(capitalArrDtl -> {
                arrDtlIdSet.add(capitalArrDtl.getId());
                // 未排款金额
                CapitalArrangement capitalArrangement = capitalArrangementService.selectById(capitalArrDtl.getHeadId());
                BigDecimal noExecuteSum = capitalArrDtl.getNoExecuteSum();
                feePayableList.stream().filter(feePayable -> FeePayableBaseEnum.NO.getCode().equals(feePayable.getIsArrPay())).forEach(feePayable -> {
                    // 判断是否部分还是全部排款
                    if (Objects.nonNull(noExecuteSum)) {
                        if (noExecuteSum.compareTo(BigDecimal.ZERO) > 0) {
                            // 未排款金额不为0 - 部分排款
                            feePayable.setIsArrPay(FeePayableBaseEnum.PART.getCode());
                        } else {
                            // 已全部排款
                            feePayable.setIsArrPay(FeePayableBaseEnum.ALL.getCode());
                        }
                    }
                    String arrPayCode = feePayable.getArrPayCode();
                    if (StringUtils.isNotBlank(arrPayCode) && !arrPayCode.contains(capitalArrangement.getCode())) {
                        feePayable.setArrPayCode(feePayable.getArrPayCode() + "; " + capitalArrangement.getCode());
                    } else {
                        feePayable.setArrPayCode(capitalArrangement.getCode());
                    }
                    feePayable.setGmtModify(new Date());
                });
            });

            // 是否付款
            EntityWrapper<PayAffirmApply> payAffirmApplyEntityWrapper = new EntityWrapper<>();
            payAffirmApplyEntityWrapper.eq("capital_dtl_id", arrDtlIdSet);
            payAffirmApplyEntityWrapper.orderBy("id asc");
            List<PayAffirmApply> payAffirmApplyList = payAffirmApplyService.selectList(payAffirmApplyEntityWrapper);

            if (CollectionUtils.isNotEmpty(payAffirmApplyList)) {
                Set<Long> payIdSet = new HashSet<>();
                payAffirmApplyList.stream()
                        .filter(payAffirmApply -> Objects.nonNull(payAffirmApply.getCapitalDtlId()))
                        .forEach(payAffirmApply -> {
                            PayAffirm payAffirm = payAffirmService.selectById(payAffirmApply.getHeadId());
                            // 未核销金额
                            BigDecimal notPaidSum = payAffirmApply.getNotPaidSum();
                            // 获取排款明细
                            CapitalArrDtl capitalArrDtl = capitalArrDtlService.selectById(payAffirmApply.getCapitalDtlId());
                            // 判断
                            if (Objects.nonNull(capitalArrDtl) && !payIdSet.contains(capitalArrDtl.getPayId())) {

                                List<Long> payIdList = Lists.newArrayList();
                                payIdList.add(capitalArrDtl.getPayId());

                                if (!CollectionUtils.isEmpty(feePayableList)) {
                                    feePayableList.stream().filter(feePayable -> FeePayableBaseEnum.NO.getCode().equals(feePayable.getIsPayment())).forEach(feePayable -> {

                                        // 正常操作
                                        // 判断是否部分还是全部付款
                                        if (Objects.nonNull(notPaidSum)) {
                                            if (notPaidSum.compareTo(BigDecimal.ZERO) > 0) {
                                                // 未核销金额不为0 - 部分核销
                                                feePayable.setIsPayment(FeePayableBaseEnum.PART.getCode());
                                            } else {
                                                // 已全部核销
                                                // feePayable.setIsPayment(FeePayableBaseEnum.ALL.getCode());
                                                // 2019-05-30 修改, 全部付款时候标识和排款标识统一
                                                feePayable.setIsPayment(feePayable.getIsArrPay());
                                            }
                                        }
                                        // 付款确认时间
                                        feePayable.setPaymentDate(payAffirm.getGmtCreate());
                                        feePayable.setGmtModify(new Date());
                                    });
                                }
                                payIdSet.add(capitalArrDtl.getPayId());
                            }
                        });
            }

            feePayableService.updateBatchById(feePayableList);
        });

        return result;
    }
}
